﻿namespace DimensionsHelper.Serialization.CommandLine;

public abstract class ArgumentContext
{
    
    public ArgumentContext()
    {
        Initialize();
    }


    protected readonly Dictionary<Type, ArgumentObject> typeObjects = new();


    protected void Register(Type type, ArgumentObject obj)
    {
        typeObjects.TryAdd(type, obj);
    }


    protected abstract void Initialize();


    /// <summary>
    /// 获取指定类型的参数对象
    /// </summary>
    public ArgumentObject? GetArgumentObject(Type type)
    {
        if (typeObjects.TryGetValue(type, out var info))
        {
            return info;
        }

        return null;
    }


    /// <summary>
    /// 将参数列表转换成指定类型。
    /// </summary>
    /// <exception cref="Exception"></exception>
    public T Resolve<T>(Func<string, string?> valueGetter)
    {
        var obj = GetArgumentObject(typeof(T)) ??
            throw new Exception($"类型'{typeof(T).Name}'未注册.");

        return (T)obj.Resolve(valueGetter);
    }

}
